<?php
/**
 * Course Class Model
 *
 * Copyright 2012
 * @date 21-April-2012
 * @author 
 * @version 0.0.1
 **/
class CustomerShopperGroup extends AppModel {
/**
 * Name
 *
 * @var string
 */
    public $name = 'CustomerShopperGroup';
    public $useTable = "customers_shopper_groups";
    public $belongsTo = array(
        'Customer'=>array(
            'className'=>'Customer',
            'foreignKey'=>'customer_id'
        ),
        'ShopperGroup'=>array(
            'className'=>'ShopperGroup',
            'foreignKey'=>'shopper_group_id'
        )
    );
    
    public function get_groups_by_customer($customer_id) {
        $this->unbindModel(array("belongsTo"=>array("Customer")));
        return $this->find("all",array('conditions'=>array('customer_id'=>$customer_id)));
    }
    public function delete_groups_by_customer($customer_id) {
        return $this->query("Delete from customers_shopper_groups where customer_id=$customer_id");
    }
    public function get_customers_by_group($group_id){
        $this->unbindModel(array("belongsTo"=>array("ShopperGroup")));
        return $this->find("all",array('conditions'=>array('shopper_group_id'=>$group_id)));
    }
    public function delete_customers_by_group($group_id) {
        return $this->query("Delete from customers_shopper_groups where shopper_group_id=$group_id");
    }
    public static function show_groups_by_customer($customer_id) {
        $csg = ClassRegistry::init("CustomerShopperGroup");
        $csg->unbindModel(array("belongsTo"=>array("Customer")));
        $groups =  $csg->find("all",array('conditions'=>array('customer_id'=>$customer_id)));
        $result = array();
        if(!empty($groups)){
            foreach ($groups as $value) {
                $result[]=array('id'=>$value['ShopperGroup']['id'],'name'=>$value['ShopperGroup']['name']);
            }
        }
        return $result;
    }
    
}